package com.byt.framlib.commonutils;

import android.content.SharedPreferences;
import android.preference.PreferenceManager;

import com.byt.framlib.baseapp.BaseApplication;


/**
 * 对SharedPreference文件中的各种类型的数据进行存取操作
 */
public class SharePerUtils {

    private static SharedPreferences sp;

    private static void init() {
        if (sp == null) {
            sp = PreferenceManager.getDefaultSharedPreferences(BaseApplication.getAppContext());
        }
    }

    public static void setSharedInt(String key, int value) {
        if (sp == null) {
            init();
        }
        sp.edit().putInt(key, value).commit();
    }

    public static int getSharedInt(String key) {
        if (sp == null) {
            init();
        }
        return sp.getInt(key, 0);
    }

    public static int getSharedInt(String key, int defValue) {
        if (sp == null) {
            init();
        }
        return sp.getInt(key, defValue);
    }

    public static void setSharedlong(String key, long value) {
        if (sp == null) {
            init();
        }
        sp.edit().putLong(key, value).commit();
    }

    public static long getSharedlong(String key) {
        if (sp == null) {
            init();
        }
        return sp.getLong(key, 0L);
    }

    public static long getSharedlongDefalt(String key,long posiitonId) {
        if (sp == null) {
            init();
        }
        return sp.getLong(key, posiitonId);
    }

    public static void setSharedFloat(String key, float value) {
        if (sp == null) {
            init();
        }
        sp.edit().putFloat(key, value).commit();
    }

    public static Float getSharedFloat(String key) {
        if (sp == null) {
            init();
        }
        return sp.getFloat(key, 0f);
    }

    public static void setSharedBoolean(String key, boolean value) {
        if (sp == null) {
            init();
        }
        sp.edit().putBoolean(key, value).commit();
    }

    public static Boolean getSharedBoolean(String key) {
        if (sp == null) {
            init();
        }
        return sp.getBoolean(key, false);
    }

    public static Boolean getSharedBooleanDefault(String key, boolean mdefault) {
        if (sp == null) {
            init();
        }
        return sp.getBoolean(key, mdefault);
    }

    public static void setSharedString(String key, String value) {
        if (sp == null) {
            init();
        }
        sp.edit().putString(key, value).commit();
    }

    public static String getSharedString(String key) {
        if (sp == null) {
            init();
        }
        return sp.getString(key, "");
    }

    public static void removeSharedString(String key) {
        if (sp == null) {
            init();
        }
        sp.edit().remove(key).commit();
    }

    public static void clearSharedString() {
        if (sp == null) {
            init();
        }
        sp.edit().clear().commit();
    }

}